﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using MongoDB.Bson;
using NUnit.Framework;

namespace MongoCamp.UnitTest
{
    [TestFixture]
    public class EntityCollectionTest
    {
        public EntityCollectionTest()
        {
            _intId = 1;
            _createdOn = DateTime.Now.ToUniversalTime();
        }

        private readonly DateTime _createdOn;
        private string _stringId;
        private readonly int _intId;

        [TearDown]
        public void Drop()
        {
            MockByInt.DorpCollection();
            MockByString.DorpCollection();
            MockByObjectId.DorpCollection();
        }

        [SetUp]
        public void SaveAndGet()
        {
            var mockInt = new MockByInt { MockId = _intId, CreatedOn = _createdOn };
            var resultInt = mockInt.Save();

            Assert.IsNotNull(resultInt);
            Assert.IsTrue(resultInt.Ok);

            _stringId = PrimaryKey.GenerateNewGuid();

            var mockString = new MockByString { MockId = _stringId, CreatedOn = _createdOn };
            var resultString = mockString.Save();

            Assert.IsNotNull(resultString);
            Assert.IsTrue(resultString.Ok);

            var mockOjbectId = new MockByObjectId { CreatedOn = _createdOn };
            // the PrimaryKey is OjbectId, so you can use insert
            var resultOjbect = MockByObjectId.Insert(mockOjbectId);

            Assert.IsNotNull(resultOjbect);
            Assert.IsTrue(resultOjbect.Ok);
        }

        [Test]
        public void FindOne()
        {
            var mockInt = MockByInt.FindOne(_intId);

            Assert.IsInstanceOf<MockByInt>(mockInt);
            Assert.AreEqual(mockInt.MockId, _intId);
            //Assert.AreEqual(_createdOn.ToString(), mockInt.CreatedOn.ToString());

            var mockString = MockByString.FindOneAsBson(_stringId);

            Assert.IsInstanceOf<BsonDocument>(mockString);
            Assert.AreEqual(mockString[PrimaryKey.Field].AsString, _stringId);
            //Assert.AreEqual(_createdOn.ToString(), mockString["CreatedOn"].ToUniversalTime().ToString());
        }


    }
}
